cct <- coeftest(regc, vcov = vcov)
regw <- (glm(cbind(full3$w_search, full3$n.x-full3$w_search)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born + date + trump_admin:trump_vote +trump_admin, data=full3, family="binomial"))
vcov <- cluster.vcov(regw, factor(full3$county.x))
cwt <- coeftest(regw, vcov = vcov)
regr <- (glm(cbind(full3$r_search, full3$n.x-full3$r_search)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born + date + trump_admin:trump_vote +trump_admin, data=full3, family="binomial"))
vcov <- cluster.vcov(regr, factor(full3$county.x))
crt <- coeftest(regr, vcov = vcov)
stargazer(cc, cw, cr, cct, cwt,crt, single.row=T, type="text")
library(data.table)
library(stringr)
library(sp)
library(maps)
library(maptools)
library(lmtest)
library(multiwayvcov)
library(naniar)
library(stm)
library(beepr)
latlong2county <- function(pointsDF) {
# Prepare SpatialPolygons object with one SpatialPolygon
# per state (plus DC, minus HI & AK)
states <- map('county', fill=TRUE, col="transparent", plot=FALSE)
IDs <- sapply(strsplit(states$names, ":"), function(x) x[1])
states_sp <- map2SpatialPolygons(states, IDs=IDs,
proj4string=CRS("+proj=longlat +datum=WGS84"))
# Convert pointsDF to a SpatialPoints object
pointsSP <- SpatialPoints(pointsDF,
proj4string=CRS("+proj=longlat +datum=WGS84"))
# Use 'over' to get _indices_ of the Polygons object containing each point
indices <- over(pointsSP, states_sp)
# Return the state names of the Polygons object containing each point
stateNames <- sapply(states_sp@polygons, function(x) x@ID)
stateNames[indices]
}
county_clean <- function(x){
x <- gsub("de kalb", "dekalb",x)
x <- gsub("du page", "dupage",x)
x <- gsub("la salle", "lasalle",x)
x <- gsub("la porte", "laporte",x)
x <- gsub("baltimore city", "baltimore",x)
x <- gsub("de soto", "desoto",x)
x <- gsub("de witt", "dewitt",x)
x <- gsub("o'brien", "obrien",x)
x <- gsub("la moure", "lamoure",x)
return(x)
}
county_clean2 <- function(x){
x <- gsub("la grange", "lagrange",x)
x <- gsub("o brien", "obrien",x)
return(x)
}
setwd("C:\\Users\\b-makrup.NORTHAMERICA\\Documents\\old_docs")
reduce <- read.csv("ReduceLegalImmigration_predictions_pw.csv")
border <- read.csv("BorderSecurity_predictions_pw.csv")
border <- read.csv("BorderSecurity_predictions_pw.csv")
fips <- read.csv("fips_county_cross.csv")
fips$state <- tolower(state.name[match(fips$state,state.abb)])
fips$name <- county_clean2(tolower(fips$name))
setwd("//msrnyc-00/Root/TVSearch/Masha/Racism/final_analysis/sec3/geog")
### clean demographics ###
c1 <- read.csv("nhgis0033_ds172_2010_county.csv")
c1$urban <- c1$H7W002/c1$H7W001
p16 <- read.csv("presvote16.csv")
p16$dpct <- p16$Democrats.2016/(p16$Democrats.2016+p16$Republicans.2016)
c1$COUNTYA <- str_pad(c1$COUNTYA,3,pad="0",side="left")
c1$Fips <- paste0(c1$STATEA, c1$COUNTYA)
p16 <- p16[,c("Fips","dpct")]
c1<- merge(c1, p16, by="Fips")
c1 <- c1[,c("Fips","STATE","COUNTY","urban","dpct")]
c2 <- read.csv("nhgis0033_ds233_20175_2017_county.csv")
c2$black<- c2$AHY2E003/c2$AHY2E001
c2$hisp <- c2$AHZAE012/c2$AHZAE001
c2$bacc <- (c2$AH04E022+c2$AH04E023+c2$AH04E024+c2$AH04E025)/c2$AH04E001
c2$medianhhinc <- c2$AH1PE001
c2 <- c2[,c("STATE","COUNTY","black","hisp","bacc","medianhhinc")]
c3 <- read.csv("nhgis0033_ds234_20175_2017_county.csv")
c3$foreign_born <- (c3$AH8VE001-(c3$AH8VE002+c3$AH8VE003))/c3$AH8VE001
c3 <- c3[,c("STATE","COUNTY","foreign_born")]
c_tot <- merge(c1,c2)
c_tot <- merge(c_tot, c3)
c_tot$COUNTY<- tolower(gsub(" County|\\.| Parish","", c_tot$COUNTY))
c_tot$STATE <- tolower(c_tot$STATE)
c_tot$county <- paste0(c_tot$STATE,",",c_tot$COUNTY)
c_tot <- c_tot[,c("Fips","urban","black","hisp","bacc","medianhhinc","foreign_born","dpct","county")]
c_tot$county <- gsub("'s","s",c_tot$county)
### read in search ###
#get daily searches per county
### CRIME ###
#terms:
#Intersect (crime|criminal|murder|kill) & (immigr|illegals|illegal alien)
setwd("~/")
bg_crime <- fread("geog_crime_raw.txt", data.table=F, header=F, quote="")
colnames(bg_crime) <- c("query","date","lat","lon")
#clean up and remove unrelated searches
#remove terms that are unrelated or botted
bg_crime <- bg_crime[grep("deer were killed by predators|sweden immigrants crime|ICE immigrant-crime hotline calls|criminal justice reform|theodore roosevelt on race, riots, immigration, and crime book",bg_crime$query,ignore.case=T, invert=T),]
bg_crime$county <- latlong2county(bg_crime[,c("lon","lat")])
bg_crime$date <- as.Date(bg_crime$date)
tmp <- bg_crime[bg_crime$date>=as.Date("2016-03-01"),]
tmp <- tmp[tmp$date>as.Date("2018-12-31")|tmp$date<as.Date("2018-12-01"),]
c_agg <- as.data.frame(table(tmp$county))
### WELFARE ###
#terms:
#Intersect (welfare|cost|benefits) & (immigr|illegals|illegal alien)
bg_welfare <- fread("geog_welfare_raw.txt", data.table=F, header=F, quote="")
colnames(bg_welfare) <- c("query","date","lat","lon")
#clean up and remove unrelated searches
#remove terms that are unrelated or botted
bg_welfare <- bg_welfare[grep("low cost immigration law services|immigration benefits|costa rica immigration|immigrant costumes|benefits of illegal immigration|process and cost for immigrants coming to usa|benefits of immigrants|low cost immigration law services orange county ca|benefits of immigration|California immigrants health benefits",bg_welfare$query,ignore.case=T, invert=T),]
bg_welfare$county <- latlong2county(bg_welfare[,c("lon","lat")])
bg_welfare$date <- as.Date(bg_welfare$date)
tmp <- bg_welfare[bg_welfare$date>=as.Date("2016-03-01"),]
tmp <- tmp[tmp$date>as.Date("2018-12-31")|tmp$date<as.Date("2018-12-01"),]
w_agg <- as.data.frame(table(tmp$county))
### REPORT ###
#terms:
#Intersect (report) & (immigr|illegals|illegal alien|to ice)
bg_report <- fread("geog_report_raw.txt", data.table=F, header=F, quote="")
colnames(bg_report) <- c("v1","query","date","v4","lat","lon","v7")
#clean up and remove unrelated searches
#remove terms that are unrelated
bg_report <- bg_report[grep("immigration commission report|: Report|us news and world report|crime report|the parents report|summarizing a report|IG Report|MSNBC Report|criminal illegal alien report|Report On|sample report|thai immigration 90 day report|report from the Government Accounting Office|intelligence report|responds to immigration report|center for immigration studies report",bg_report$query,ignore.case=T, invert=T),]
bg_report$county <- latlong2county(bg_report[,c("lon","lat")])
bg_report$date <- as.Date(bg_report$date)
tmp <- bg_report[bg_report$date>=as.Date("2016-03-01"),]
tmp <- tmp[tmp$date>as.Date("2018-12-31")|tmp$date<as.Date("2018-12-01"),]
r_agg <- as.data.frame(table(tmp$county))
### HSI ###
#terms:
#searches that led to at least one click to following url:
#https://www.ice.gov/webform/hsi-tip-form
bg_hsi <- fread("geog_hsi_raw.txt", data.table=F, header=F, quote="")
colnames(bg_hsi) <- c("date","query","lat","lon")
bg_hsi <- na.omit(bg_hsi)
bg_hsi$county <- latlong2county(bg_hsi[,c("lon","lat")])
bg_hsi$date <- as.Date(bg_hsi$date)
tmp <- bg_hsi[bg_hsi$date>=as.Date("2016-03-01"),]
tmp <- tmp[tmp$date>as.Date("2018-12-31")|tmp$date<as.Date("2018-12-01"),]
h_agg <- as.data.frame(table(tmp$county))
### Denominators ###
d1 <- fread("county_denoms_1.txt")
d2 <- fread("county_denoms_2.txt")
d3 <- fread("county_denoms_3.txt")
d4 <- fread("county_denoms_4.txt")
d5 <- fread("county_denoms_5.txt")
d5 <- d5[,c("V2","V3","V4","V1")]
colnames(d1)<- c("date","lat","lon","n")
d1$county <- latlong2county(d1[,c("lon","lat")])
colnames(d2)<- c("date","lat","lon","n")
d2$county <- latlong2county(d2[,c("lon","lat")])
colnames(d3)<- c("date","lat","lon","n")
d3$county <- latlong2county(d3[,c("lon","lat")])
colnames(d4)<- c("date","lat","lon","n")
d4$county <- latlong2county(d4[,c("lon","lat")])
colnames(d5)<- c("date","lat","lon","n")
d5 <- na.omit(d5)
d5$county <- latlong2county(d5[,c("lon","lat")])
d1 <- na.omit(d1[,c("date","county","n")])
d2 <- na.omit(d2[,c("date","county","n")])
d3 <- na.omit(d3[,c("date","county","n")])
d4 <- na.omit(d4[,c("date","county","n")])
d5 <- na.omit(d5[,c("date","county","n")])
d1$date <- as.Date(d1$date)
d2$date <- as.Date(d2$date)
d3$date <- as.Date(d3$date)
d4$date <- as.Date(d4$date)
d5$date <- as.Date(d5$date)
#fixing dates to make sure everything overlaps
d1 <- d1[d1$date>=as.Date("2016-03-01"),]
d1 <- d1[d1$date<=as.Date("2019-07-31"),]
d1 <- d1[d1$date>as.Date("2018-12-31")|d1$date<as.Date("2018-12-01"),]
d2 <- d2[d2$date>=as.Date("2016-03-01"),]
d2 <- d2[d2$date<=as.Date("2019-07-31"),]
d2 <- d2[d2$date>as.Date("2018-12-31")|d2$date<as.Date("2018-12-01"),]
d3 <- d3[d3$date>=as.Date("2016-03-01"),]
d3 <- d3[d3$date<=as.Date("2019-07-31"),]
d3 <- d3[d3$date>as.Date("2018-12-31")|d3$date<as.Date("2018-12-01"),]
d4 <- d4[d4$date>=as.Date("2016-03-01"),]
d4 <- d4[d4$date<=as.Date("2019-07-31"),]
d4 <- d4[d4$date>as.Date("2018-12-31")|d4$date<as.Date("2018-12-01"),]
d5 <- d5[d5$date>=as.Date("2016-03-01"),]
d5 <- d5[d5$date<=as.Date("2019-07-31"),]
d5 <- d5[d5$date>as.Date("2018-12-31")|d5$date<as.Date("2018-12-01"),]
agg1 <- aggregate(d1$n, list(d1$county), sum)
agg2 <- aggregate(d2$n, list(d2$county), sum)
agg3 <- aggregate(d3$n, list(d3$county), sum)
agg4 <- aggregate(d4$n, list(d4$county), sum)
agg5 <- aggregate(d5$n, list(d5$county), sum)
denom <- rbind(agg1,agg2,agg3,agg4,agg5)
rm(d1,d2,d3,d4,d5)
colnames(denom) <- c("county","denom")
denom <- aggregate(denom$denom, list(denom$county), sum)
colnames(denom) <- c("county","denom")
colnames(c_agg) <- c("county","crime")
colnames(w_agg) <- c("county","welfare")
colnames(r_agg) <- c("county","report")
colnames(h_agg) <- c("county","hsi")
s_agg <- merge(h_agg,merge(r_agg,merge(c_agg, w_agg, by=c("county"),all=T), by=c("county"),all=T), by=c("county"),all=T)
full <- merge(s_agg, denom, by=c("county"), all=T)
fips$county <- tolower(paste0(fips$state,",",fips$name))
fips <- merge(fips, reduce, by.x="fips", by.y="DemoGroup1")
full0 <- merge(full, fips, by="county")
full2 <- merge(full0, c_tot, by.x="fips", by.y="Fips")
#drop county obs with no denom obs?
full2 <- full2[!is.na(full2$denom),]
full2$crime[is.na(full2$crime)]<- 0
full2$welfare[is.na(full2$welfare)]<- 0
full2$hsi[is.na(full2$hsi)]<- 0
full2$report[is.na(full2$report)]<- 0
full2$crime_prop <- full2$crime/full2$denom
full2$welfare_prop <- full2$welfare/full2$denom
full2$report_prop <- full2$report/full2$denom
full2$hsi_prop <- full2$hsi/full2$denom
full2$oppose_immigration <- -1*full2$p
#regc <- (glm(cbind(full2$crime, full2$denom-full2$crime)~ urban + black +medianhhinc + hisp + bacc+ oppose_immigration + foreign_born + oppose_immigration:foreign_born, data=full2, family="binomial"))
regc <- (glm(cbind(full2$crime, full2$denom-full2$crime)~ urban + black +medianhhinc + hisp + bacc+ oppose_immigration + foreign_born, data=full2, family="binomial"))
vcov <- cluster.vcov(regc, factor(full2$county.x))
cc <- coeftest(regc, vcov = vcov)
#regw <- (glm(cbind(full2$welfare, full2$denom-full2$welfare)~ urban + black +medianhhinc + hisp + bacc+ oppose_immigration + foreign_born + oppose_immigration:foreign_born, data=full2, family="binomial"))
regw <- (glm(cbind(full2$welfare, full2$denom-full2$welfare)~ urban + black +medianhhinc + hisp + bacc+ oppose_immigration + foreign_born, data=full2, family="binomial"))
vcov <- cluster.vcov(regw, factor(full2$county.x))
cw <- coeftest(regw, vcov = vcov)
#regr <- (glm(cbind(full2$report, full2$denom-full2$report)~ urban + black +medianhhinc + hisp + bacc+ oppose_immigration + foreign_born + oppose_immigration:foreign_born, data=full2, family="binomial"))
regr <- (glm(cbind(full2$report, full2$denom-full2$report)~ urban + black +medianhhinc + hisp + bacc+ oppose_immigration + foreign_born, data=full2, family="binomial"))
vcov <- cluster.vcov(regr, factor(full2$county.x))
cr <- coeftest(regr, vcov = vcov)
#regh <- (glm(cbind(full2$hsi, full2$denom-full2$hsi)~ urban + black +medianhhinc + hisp + bacc+ oppose_immigration + foreign_born + oppose_immigration:foreign_born, data=full2, family="binomial"))
regh <- (glm(cbind(full2$hsi, full2$denom-full2$hsi)~ urban + black +medianhhinc + hisp + bacc+ oppose_immigration + foreign_born, data=full2, family="binomial"))
vcov <- cluster.vcov(regh, factor(full2$county.x))
ch <- coeftest(regh, vcov = vcov)
full2$trump_vote <- 1-full2$dpct
#regc <- (glm(cbind(full2$crime, full2$denom-full2$crime)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born + trump_vote:foreign_born, data=full2, family="binomial"))
regc <- (glm(cbind(full2$crime, full2$denom-full2$crime)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born, data=full2, family="binomial"))
vcov <- cluster.vcov(regc, factor(full2$county.x))
cct <- coeftest(regc, vcov = vcov)
#regw <- (glm(cbind(full2$welfare, full2$denom-full2$welfare)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born + trump_vote:foreign_born, data=full2, family="binomial"))
regw <- (glm(cbind(full2$welfare, full2$denom-full2$welfare)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born, data=full2, family="binomial"))
vcov <- cluster.vcov(regw, factor(full2$county.x))
cwt <- coeftest(regw, vcov = vcov)
#regr <- (glm(cbind(full2$report, full2$denom-full2$report)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born + trump_vote:foreign_born, data=full2, family="binomial"))
regr <- (glm(cbind(full2$report, full2$denom-full2$report)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born, data=full2, family="binomial"))
vcov <- cluster.vcov(regr, factor(full2$county.x))
crt <- coeftest(regr, vcov = vcov)
#regh <- (glm(cbind(full2$hsi, full2$denom-full2$hsi)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born + trump_vote:foreign_born, data=full2, family="binomial"))
regh <- (glm(cbind(full2$hsi, full2$denom-full2$hsi)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born, data=full2, family="binomial"))
vcov <- cluster.vcov(regh, factor(full2$county.x))
cht <- coeftest(regh, vcov = vcov)
library(stargazer)
stargazer(cc, cct, cw, cwt,cr, crt, single.row=T, type="text")
library(data.table)
library(stringr)
library(sp)
library(maps)
library(maptools)
library(lmtest)
library(multiwayvcov)
library(naniar)
library(stm)
library(beepr)
latlong2county <- function(pointsDF) {
# Prepare SpatialPolygons object with one SpatialPolygon
# per state (plus DC, minus HI & AK)
states <- map('county', fill=TRUE, col="transparent", plot=FALSE)
IDs <- sapply(strsplit(states$names, ":"), function(x) x[1])
states_sp <- map2SpatialPolygons(states, IDs=IDs,
proj4string=CRS("+proj=longlat +datum=WGS84"))
# Convert pointsDF to a SpatialPoints object
pointsSP <- SpatialPoints(pointsDF,
proj4string=CRS("+proj=longlat +datum=WGS84"))
# Use 'over' to get _indices_ of the Polygons object containing each point
indices <- over(pointsSP, states_sp)
# Return the state names of the Polygons object containing each point
stateNames <- sapply(states_sp@polygons, function(x) x@ID)
stateNames[indices]
}
county_clean <- function(x){
x <- gsub("de kalb", "dekalb",x)
x <- gsub("du page", "dupage",x)
x <- gsub("la salle", "lasalle",x)
x <- gsub("la porte", "laporte",x)
x <- gsub("baltimore city", "baltimore",x)
x <- gsub("de soto", "desoto",x)
x <- gsub("de witt", "dewitt",x)
x <- gsub("o'brien", "obrien",x)
x <- gsub("la moure", "lamoure",x)
return(x)
}
county_clean2 <- function(x){
x <- gsub("la grange", "lagrange",x)
x <- gsub("o brien", "obrien",x)
return(x)
}
setwd("C:\\Users\\b-makrup.NORTHAMERICA\\Documents\\old_docs")
reduce <- read.csv("ReduceLegalImmigration_predictions_pw.csv")
border <- read.csv("BorderSecurity_predictions_pw.csv")
border <- read.csv("BorderSecurity_predictions_pw.csv")
fips <- read.csv("fips_county_cross.csv")
fips$state <- tolower(state.name[match(fips$state,state.abb)])
fips$name <- county_clean2(tolower(fips$name))
setwd("//msrnyc-00/Root/TVSearch/Masha/Racism/final_analysis/sec3/geog")
### clean demographics ###
c1 <- read.csv("nhgis0033_ds172_2010_county.csv")
c1$urban <- c1$H7W002/c1$H7W001
p16 <- read.csv("presvote16.csv")
p16$dpct <- p16$Democrats.2016/(p16$Democrats.2016+p16$Republicans.2016)
c1$COUNTYA <- str_pad(c1$COUNTYA,3,pad="0",side="left")
c1$Fips <- paste0(c1$STATEA, c1$COUNTYA)
p16 <- p16[,c("Fips","dpct")]
c1<- merge(c1, p16, by="Fips")
c1 <- c1[,c("Fips","STATE","COUNTY","urban","dpct")]
c2 <- read.csv("nhgis0033_ds233_20175_2017_county.csv")
c2$black<- c2$AHY2E003/c2$AHY2E001
c2$hisp <- c2$AHZAE012/c2$AHZAE001
c2$bacc <- (c2$AH04E022+c2$AH04E023+c2$AH04E024+c2$AH04E025)/c2$AH04E001
c2$medianhhinc <- c2$AH1PE001
c2 <- c2[,c("STATE","COUNTY","black","hisp","bacc","medianhhinc")]
c3 <- read.csv("nhgis0033_ds234_20175_2017_county.csv")
c3$foreign_born <- (c3$AH8VE001-(c3$AH8VE002+c3$AH8VE003))/c3$AH8VE001
c3 <- c3[,c("STATE","COUNTY","foreign_born")]
c_tot <- merge(c1,c2)
c_tot <- merge(c_tot, c3)
c_tot$COUNTY<- tolower(gsub(" County|\\.| Parish","", c_tot$COUNTY))
c_tot$STATE <- tolower(c_tot$STATE)
c_tot$county <- paste0(c_tot$STATE,",",c_tot$COUNTY)
c_tot <- c_tot[,c("Fips","urban","black","hisp","bacc","medianhhinc","foreign_born","dpct","county")]
c_tot$county <- gsub("'s","s",c_tot$county)
### read in search ###
#get daily searches per county
### CRIME ###
#terms:
#Intersect (crime|criminal|murder|kill) & (immigr|illegals|illegal alien)
setwd("~/")
bg_crime <- fread("geog_crime_raw.txt", data.table=F, header=F, quote="")
colnames(bg_crime) <- c("query","date","lat","lon")
#clean up and remove unrelated searches
#remove terms that are unrelated or botted
bg_crime <- bg_crime[grep("deer were killed by predators|sweden immigrants crime|ICE immigrant-crime hotline calls|criminal justice reform|theodore roosevelt on race, riots, immigration, and crime book",bg_crime$query,ignore.case=T, invert=T),]
bg_crime$county <- latlong2county(bg_crime[,c("lon","lat")])
bg_crime$date <- as.Date(bg_crime$date)
tmp <- bg_crime[bg_crime$date>=as.Date("2016-03-01"),]
tmp <- tmp[tmp$date>as.Date("2018-12-31")|tmp$date<as.Date("2018-12-01"),]
c_agg <- as.data.frame(table(tmp$county))
### WELFARE ###
#terms:
#Intersect (welfare|cost|benefits) & (immigr|illegals|illegal alien)
bg_welfare <- fread("geog_welfare_raw.txt", data.table=F, header=F, quote="")
colnames(bg_welfare) <- c("query","date","lat","lon")
#clean up and remove unrelated searches
#remove terms that are unrelated or botted
bg_welfare <- bg_welfare[grep("low cost immigration law services|immigration benefits|costa rica immigration|immigrant costumes|benefits of illegal immigration|process and cost for immigrants coming to usa|benefits of immigrants|low cost immigration law services orange county ca|benefits of immigration|California immigrants health benefits",bg_welfare$query,ignore.case=T, invert=T),]
bg_welfare$county <- latlong2county(bg_welfare[,c("lon","lat")])
bg_welfare$date <- as.Date(bg_welfare$date)
tmp <- bg_welfare[bg_welfare$date>=as.Date("2016-03-01"),]
tmp <- tmp[tmp$date>as.Date("2018-12-31")|tmp$date<as.Date("2018-12-01"),]
w_agg <- as.data.frame(table(tmp$county))
### REPORT ###
#terms:
#Intersect (report) & (immigr|illegals|illegal alien|to ice)
bg_report <- fread("geog_report_raw.txt", data.table=F, header=F, quote="")
colnames(bg_report) <- c("v1","query","date","v4","lat","lon","v7")
#clean up and remove unrelated searches
#remove terms that are unrelated
bg_report <- bg_report[grep("immigration commission report|: Report|us news and world report|crime report|the parents report|summarizing a report|IG Report|MSNBC Report|criminal illegal alien report|Report On|sample report|thai immigration 90 day report|report from the Government Accounting Office|intelligence report|responds to immigration report|center for immigration studies report",bg_report$query,ignore.case=T, invert=T),]
bg_report$county <- latlong2county(bg_report[,c("lon","lat")])
bg_report$date <- as.Date(bg_report$date)
tmp <- bg_report[bg_report$date>=as.Date("2016-03-01"),]
tmp <- tmp[tmp$date>as.Date("2018-12-31")|tmp$date<as.Date("2018-12-01"),]
r_agg <- as.data.frame(table(tmp$county))
### HSI ###
#terms:
#searches that led to at least one click to following url:
#https://www.ice.gov/webform/hsi-tip-form
bg_hsi <- fread("geog_hsi_raw.txt", data.table=F, header=F, quote="")
colnames(bg_hsi) <- c("date","query","lat","lon")
bg_hsi <- na.omit(bg_hsi)
bg_hsi$county <- latlong2county(bg_hsi[,c("lon","lat")])
bg_hsi$date <- as.Date(bg_hsi$date)
tmp <- bg_hsi[bg_hsi$date>=as.Date("2016-03-01"),]
tmp <- tmp[tmp$date>as.Date("2018-12-31")|tmp$date<as.Date("2018-12-01"),]
h_agg <- as.data.frame(table(tmp$county))
### Denominators ###
d1 <- fread("county_denoms_1.txt")
d2 <- fread("county_denoms_2.txt")
d3 <- fread("county_denoms_3.txt")
d4 <- fread("county_denoms_4.txt")
d5 <- fread("county_denoms_5.txt")
d5 <- d5[,c("V2","V3","V4","V1")]
colnames(d1)<- c("date","lat","lon","n")
d1$county <- latlong2county(d1[,c("lon","lat")])
colnames(d2)<- c("date","lat","lon","n")
d2$county <- latlong2county(d2[,c("lon","lat")])
colnames(d3)<- c("date","lat","lon","n")
d3$county <- latlong2county(d3[,c("lon","lat")])
colnames(d4)<- c("date","lat","lon","n")
d4$county <- latlong2county(d4[,c("lon","lat")])
colnames(d5)<- c("date","lat","lon","n")
d5 <- na.omit(d5)
d5$county <- latlong2county(d5[,c("lon","lat")])
d1 <- na.omit(d1[,c("date","county","n")])
d2 <- na.omit(d2[,c("date","county","n")])
d3 <- na.omit(d3[,c("date","county","n")])
d4 <- na.omit(d4[,c("date","county","n")])
d5 <- na.omit(d5[,c("date","county","n")])
d1$date <- as.Date(d1$date)
d2$date <- as.Date(d2$date)
d3$date <- as.Date(d3$date)
d4$date <- as.Date(d4$date)
d5$date <- as.Date(d5$date)
#fixing dates to make sure everything overlaps
d1 <- d1[d1$date>=as.Date("2016-03-01"),]
d1 <- d1[d1$date<=as.Date("2019-07-31"),]
d1 <- d1[d1$date>as.Date("2018-12-31")|d1$date<as.Date("2018-12-01"),]
d2 <- d2[d2$date>=as.Date("2016-03-01"),]
d2 <- d2[d2$date<=as.Date("2019-07-31"),]
d2 <- d2[d2$date>as.Date("2018-12-31")|d2$date<as.Date("2018-12-01"),]
d3 <- d3[d3$date>=as.Date("2016-03-01"),]
d3 <- d3[d3$date<=as.Date("2019-07-31"),]
d3 <- d3[d3$date>as.Date("2018-12-31")|d3$date<as.Date("2018-12-01"),]
d4 <- d4[d4$date>=as.Date("2016-03-01"),]
d4 <- d4[d4$date<=as.Date("2019-07-31"),]
d4 <- d4[d4$date>as.Date("2018-12-31")|d4$date<as.Date("2018-12-01"),]
d5 <- d5[d5$date>=as.Date("2016-03-01"),]
d5 <- d5[d5$date<=as.Date("2019-07-31"),]
d5 <- d5[d5$date>as.Date("2018-12-31")|d5$date<as.Date("2018-12-01"),]
agg1 <- aggregate(d1$n, list(d1$county), sum)
agg2 <- aggregate(d2$n, list(d2$county), sum)
agg3 <- aggregate(d3$n, list(d3$county), sum)
agg4 <- aggregate(d4$n, list(d4$county), sum)
agg5 <- aggregate(d5$n, list(d5$county), sum)
denom <- rbind(agg1,agg2,agg3,agg4,agg5)
rm(d1,d2,d3,d4,d5)
colnames(denom) <- c("county","denom")
denom <- aggregate(denom$denom, list(denom$county), sum)
colnames(denom) <- c("county","denom")
colnames(c_agg) <- c("county","crime")
colnames(w_agg) <- c("county","welfare")
colnames(r_agg) <- c("county","report")
colnames(h_agg) <- c("county","hsi")
s_agg <- merge(h_agg,merge(r_agg,merge(c_agg, w_agg, by=c("county"),all=T), by=c("county"),all=T), by=c("county"),all=T)
full <- merge(s_agg, denom, by=c("county"), all=T)
fips$county <- tolower(paste0(fips$state,",",fips$name))
fips <- merge(fips, reduce, by.x="fips", by.y="DemoGroup1")
full0 <- merge(full, fips, by="county")
full2 <- merge(full0, c_tot, by.x="fips", by.y="Fips")
#drop county obs with no denom obs?
full2 <- full2[!is.na(full2$denom),]
full2$crime[is.na(full2$crime)]<- 0
full2$welfare[is.na(full2$welfare)]<- 0
full2$hsi[is.na(full2$hsi)]<- 0
full2$report[is.na(full2$report)]<- 0
full2$crime_prop <- full2$crime/full2$denom
full2$welfare_prop <- full2$welfare/full2$denom
full2$report_prop <- full2$report/full2$denom
full2$hsi_prop <- full2$hsi/full2$denom
full2$oppose_immigration <- -1*full2$p
#regc <- (glm(cbind(full2$crime, full2$denom-full2$crime)~ urban + black +medianhhinc + hisp + bacc+ oppose_immigration + foreign_born + oppose_immigration:foreign_born, data=full2, family="binomial"))
regc <- (glm(cbind(full2$crime, full2$denom-full2$crime)~ urban + black +medianhhinc + hisp + bacc+ oppose_immigration + foreign_born, data=full2, family="binomial"))
vcov <- cluster.vcov(regc, factor(full2$county.x))
cc <- coeftest(regc, vcov = vcov)
#regw <- (glm(cbind(full2$welfare, full2$denom-full2$welfare)~ urban + black +medianhhinc + hisp + bacc+ oppose_immigration + foreign_born + oppose_immigration:foreign_born, data=full2, family="binomial"))
regw <- (glm(cbind(full2$welfare, full2$denom-full2$welfare)~ urban + black +medianhhinc + hisp + bacc+ oppose_immigration + foreign_born, data=full2, family="binomial"))
vcov <- cluster.vcov(regw, factor(full2$county.x))
cw <- coeftest(regw, vcov = vcov)
#regr <- (glm(cbind(full2$report, full2$denom-full2$report)~ urban + black +medianhhinc + hisp + bacc+ oppose_immigration + foreign_born + oppose_immigration:foreign_born, data=full2, family="binomial"))
regr <- (glm(cbind(full2$report, full2$denom-full2$report)~ urban + black +medianhhinc + hisp + bacc+ oppose_immigration + foreign_born, data=full2, family="binomial"))
vcov <- cluster.vcov(regr, factor(full2$county.x))
cr <- coeftest(regr, vcov = vcov)
#regh <- (glm(cbind(full2$hsi, full2$denom-full2$hsi)~ urban + black +medianhhinc + hisp + bacc+ oppose_immigration + foreign_born + oppose_immigration:foreign_born, data=full2, family="binomial"))
regh <- (glm(cbind(full2$hsi, full2$denom-full2$hsi)~ urban + black +medianhhinc + hisp + bacc+ oppose_immigration + foreign_born, data=full2, family="binomial"))
vcov <- cluster.vcov(regh, factor(full2$county.x))
ch <- coeftest(regh, vcov = vcov)
full2$trump_vote <- 1-full2$dpct
#regc <- (glm(cbind(full2$crime, full2$denom-full2$crime)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born + trump_vote:foreign_born, data=full2, family="binomial"))
regc <- (glm(cbind(full2$crime, full2$denom-full2$crime)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born, data=full2, family="binomial"))
vcov <- cluster.vcov(regc, factor(full2$county.x))
cct <- coeftest(regc, vcov = vcov)
#regw <- (glm(cbind(full2$welfare, full2$denom-full2$welfare)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born + trump_vote:foreign_born, data=full2, family="binomial"))
regw <- (glm(cbind(full2$welfare, full2$denom-full2$welfare)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born, data=full2, family="binomial"))
vcov <- cluster.vcov(regw, factor(full2$county.x))
cwt <- coeftest(regw, vcov = vcov)
#regr <- (glm(cbind(full2$report, full2$denom-full2$report)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born + trump_vote:foreign_born, data=full2, family="binomial"))
regr <- (glm(cbind(full2$report, full2$denom-full2$report)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born, data=full2, family="binomial"))
vcov <- cluster.vcov(regr, factor(full2$county.x))
crt <- coeftest(regr, vcov = vcov)
#regh <- (glm(cbind(full2$hsi, full2$denom-full2$hsi)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born + trump_vote:foreign_born, data=full2, family="binomial"))
regh <- (glm(cbind(full2$hsi, full2$denom-full2$hsi)~ urban + black +medianhhinc + hisp + bacc+ trump_vote + foreign_born, data=full2, family="binomial"))
vcov <- cluster.vcov(regh, factor(full2$county.x))
cht <- coeftest(regh, vcov = vcov)
library(stargazer)
stargazer(cc, cct, cw, cwt,cr, crt, single.row=T, type="text")
#stargazer(cr, crt, ch, cht, single.row=T, type="text")
install.packages("here")
here()
library(here)
here()
i_am("GallupPlotReplication.R")
setwd(here("data"))
car <- read.csv("car_EconSurvey_release.csv")
house <- read.csv("house_EconSurvey_release.csv")
stock <- read.csv("stock_EconSurvey_release.csv")
library(lubridate)
library(ggplot2)
library(here)
setwd(here("data"))
here()
setwd("C:\\Users\b-makrup.NORTHAMERICA\Documents\ImmigrationReplicationFull\restricted_data")
setwd("C:\\Users\b-makrup.NORTHAMERICA\\Documents\ImmigrationReplicationFull\restricted_data")
setwd("C:\\Users\b-makrup.NORTHAMERICA\\Documents\\ImmigrationReplicationFull\restricted_data")
setwd("C:\\Users\\b-makrup.NORTHAMERICA\\Documents\\ImmigrationReplicationFull\\restricted_data")
list.files()
setwd("C:\\Users\\b-makrup.NORTHAMERICA\\Documents\\ImmigrationReplicationFull\\release_data\\SOTU\\weather")
list.files()
